package com.hb.trade.server.project.notice.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.hb.core.pojo.enums.DeployStatusEnum;
import com.hb.core.pojo.parent.MPCommonEntity;
import com.hb.core.pojo.project.notice.dto.NoticeDTO;
import com.hb.core.pojo.project.notice.entity.Notice;
import com.hb.core.pojo.project.notice.entity.NoticeBidSection;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.trade.server.project.notice.service.NoticeBidSectionService;
import com.hb.trade.server.project.notice.service.NoticeService;
import com.hb.trade.server.project.section.service.raw.BidSectionService;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.enums.LogicEnum;
import org.fzzn.core.util.Enums;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 *
 * @Description: 延期公告及实现逻辑
 **/
@Service
@Slf4j
public class NoticeSaveDelay extends NoticeSaveSection {

    protected final BidSectionService bidSectionService;
    protected final NoticeBidSectionService noticeBidSectionService;

    public NoticeSaveDelay(NoticeService noticeService,
                           BidSectionService bidSectionService,
                           NoticeBidSectionService noticeBidSectionService) {
        super(noticeService);
        this.bidSectionService = bidSectionService;
        this.noticeBidSectionService = noticeBidSectionService;
    }

    /**
     * 对公告执行保存
     *
     * @param noticeDTO
     * @return 保存公告结果, true: 保存成功, false: 保存失败
     */
    @Override
    public boolean saveNotice(NoticeDTO noticeDTO) {
//        // 标段属性设置
//        BidSectionDTO options = noticeDTO.getSectionOptions();
//        if (options != null) {
//            JSONConfig config = JSONConfig.create();
//            config.setDateFormat(DatePattern.NORM_DATETIME_PATTERN);
//            JSONObject sectionJSON = JSONUtil.parseObj(options);
//            noticeDTO.setSectionInfo(sectionJSON.toString());
//        }

        // 处理公告主体信息
        Long noticeId = noticeDTO.getId();
        if (noticeId != null) {
            DeployStatusEnum status = getStatus(noticeId);
            if (status == DeployStatusEnum.DEPLOYED) {
                log.warn("无法修改已发布延期的公告");
                return false;
            }

            Notice rawNotice = noticeService.getById(noticeId);
            noticeDTO.to(rawNotice);
            noticeService.updateById(rawNotice);
        } else {
            // 公告发布状态默认为未发布
            noticeDTO.setDeployStatus(DeployStatusEnum.UN_DEPLOYED.getCode());
            noticeDTO.buildBaseData(1);
            noticeService.save(noticeDTO);
        }

        // 处理标段信息
        List<BidSectionDTO> sections = noticeDTO.getBidSectionList();
        // 处理公告跟标段的关联
        List<Long> sectionIds = CollUtil.map(sections, MPCommonEntity::getId, true);


        if (CollUtil.isNotEmpty(sectionIds)) {
            // 更新标段, 公告中间表
            noticeBidSectionService.updateNoticeSections(noticeDTO.getId(), sectionIds);
        }


        return true;
    }

    @Override
    public String deploy(Notice notice) {
        List<NoticeBidSection> noticeBidSections = noticeBidSectionService.listNoticeBidSectionByNoticeId(notice.getId());
        if (CollUtil.isEmpty(noticeBidSections)){
            return null;
        }
        //判断是否需要重新编制招标文件
        //如果需要重新编制招标文件
        if (Enums.equals(LogicEnum.YES,notice.getAgainFileFlag())){
            //置空招标文件的标段信息
            for (NoticeBidSection noticeBidSection : noticeBidSections) {
                bidSectionService.cancelRelateFile(noticeBidSection.getSectionId());
            }
        }
        BidSectionDTO sectionOptions = BeanUtil.copyProperties(notice.getSectionOptions(),BidSectionDTO.class);
        if (sectionOptions == null){
            return null;
        }
        //如果不需要重新编制招标文件 更新招标文件的信息 则需要把公告的标段信息同步到标段中
        for (NoticeBidSection noticeBidSection : noticeBidSections) {
            sectionOptions.setId(noticeBidSection.getSectionId());
            bidSectionService.updateById(sectionOptions);
        }


        return null;
    }
}
